package com.Sdebruijn.curriculumvitae.asynctask;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Sdebruijn.curriculumvitae.R;
import com.Sdebruijn.curriculumvitae.db.DatabaseManager;
import com.Sdebruijn.curriculumvitae.fragments.EducationDataFragment;
import com.Sdebruijn.curriculumvitae.models.Education;
import com.Sdebruijn.curriculumvitae.models.Experience;
import com.Sdebruijn.curriculumvitae.util.ViewUtils;
import com.Sdebruijn.curriculumvitae.view.ProgressWheel;
import com.Sdebruijn.curriculumvitae.view.RemoteImageView;

/**
 * Loads the data of a Person in a separate background thread with
 * {@link AsyncTask}
 * 
 * @author Stefan de Bruijn
 */
// TODO Eventually replace this with a service - per example with RoboSpice
public class LoadEducationDataAsyncTask extends AsyncTask<Void, Void, List<Experience>> {

	/*
	 * Data fields from the fragment, in order according to the view
	 */
	private LinearLayout ll_experience_data;

	private RemoteImageView riv_experience_block_logo;
	private TextView tv_experience_block_name;
	private TextView tv_experience_block_description;
	private TextView tv_experience_block_start_date;
	private TextView tv_experience_block_end_date;
	private TextView tv_experience_block_quick_description;

	private ProgressWheel pg;
	private Context context;

	public LoadEducationDataAsyncTask(View view, Context context) {
		this.context = context;

		ll_experience_data = (LinearLayout) view.findViewById(R.id.ll_experience_data);
		pg = (ProgressWheel) view.findViewById(R.id.pw_spinner);

	}

	@Override
	protected void onPreExecute() {
		// Show the progress wheel and make it spin
		ViewUtils.switchVisibleState(ll_experience_data, ViewUtils.State.INVISIBLE, pg,
				ViewUtils.State.VISIBLE);
		pg.spin();
	}

	@Override
	protected List<Experience> doInBackground(Void... params) {
		return DatabaseManager.getInstance().getAllEducation();
	}

	@Override
	protected void onPostExecute(List<Experience> results) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		for (Experience experience : results) {
			Education education = (Education) experience;

			View vExperienceBlock = inflater.inflate(R.layout.merge_experience_block_shaded, null);

			riv_experience_block_logo = (RemoteImageView) vExperienceBlock
					.findViewById(R.id.riv_experience_block_logo);
			tv_experience_block_name = (TextView) vExperienceBlock
					.findViewById(R.id.tv_experience_block_name);
			tv_experience_block_description = (TextView) vExperienceBlock
					.findViewById(R.id.tv_experience_block_description);
			tv_experience_block_start_date = (TextView) vExperienceBlock
					.findViewById(R.id.tv_experience_block_start_date);
			tv_experience_block_end_date = (TextView) vExperienceBlock
					.findViewById(R.id.tv_experience_block_end_date);
			tv_experience_block_quick_description = (TextView) vExperienceBlock
					.findViewById(R.id.tv_experience_block_quick_description);

			riv_experience_block_logo.setRemotePath(education.institution.logo);
			riv_experience_block_logo.loadImage(true);

			tv_experience_block_name.setText(education.institution.name_short);
			tv_experience_block_description.setText(education.institution.description);

			tv_experience_block_start_date.setText(education.start_date);
			tv_experience_block_end_date.setText(education.end_date);
			tv_experience_block_quick_description.setText(education.niveau);

			ll_experience_data.addView(vExperienceBlock);

		}

		ViewUtils.switchVisibleState(ll_experience_data, ViewUtils.State.VISIBLE, pg,
				ViewUtils.State.INVISIBLE);

		pg.stopSpinning();
		EducationDataFragment.mEducation = results;
	}
}
